package archimede;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JTextField;

import net.miginfocom.swing.MigLayout;


/**
 * barra dei pulsanti per la tab PIANTINA CON PUNTI DI RILIEVO
 * 
 * @author root
 * 
 */
public class BarraPulsanti extends JPanel {

	// tipo oggetto
	private static final int p = 1;
	private static final int m = 2;
	private static final int c = 3;
	private static final int t = 4;
	private static final int q = 5;
	private static final int n = 6;

	// direzione oggetto
	private static final int up = 0;
	private static final int down = 1;
	private static final int right = 2;
	private static final int left = 3;
	private static final int degree45 = 4;
	private static final int degree135 = 5;
	private static final int degree225 = 6;
	private static final int degree315 = 7;
	private static final int none = 8;

	/**
	 * pannello contenente l'immagine
	 */
	private Pannello panel;

	private PannelloPiantina piantina;

	/**
	 * settore per cambiare titolo alla tab
	 */
	private JTextField piano = new JTextField();

	/**
	 * riferimento al frame principale
	 */
	private StartingWindow st;

	/**
	 * contatore delle tab seriale
	 */
	private int countM = 1;

	/**
	 * true : se sostituisco l'immagine
	 */
	private boolean replace = false;

	/**
	 * ultimo percorso locale aperto
	 */
	private String lastPath = "";

	public BarraPulsanti(final PannelloPiantina piantina, final StartingWindow st) {
		super();
		this.piantina = piantina;
		this.st = st;
		setLayout(new MigLayout());
		setOpaque(false);

		// bottone per caricare piantina
		final JButton upload = new JButton("carica piantina");
		AbstractAction uploadAct = new AbstractAction("upload") {
			public void actionPerformed(ActionEvent e) {
				try {
					// scelta dell'immagine
					JFileChooser chooser = new JFileChooser();
					ImagePreviewPanel preview = new ImagePreviewPanel();
					chooser.setAccessory(preview);
					chooser.addPropertyChangeListener(preview);
					lastPath = st.getPath();
					if (lastPath.equals(""))
						chooser.getCurrentDirectory();
					else
						chooser.setCurrentDirectory(new File(lastPath));
					chooser.showOpenDialog(upload);
					// prendo il percorso della foto
					String sourcePath = chooser.getSelectedFile().toString();
					st.setPath(sourcePath);
					lastPath = sourcePath;
					// creo immagine
					ImageIcon tmp = new ImageIcon(sourcePath);
					// prima immagine
					if (!replace) {
						// resize
						if (tmp.getIconWidth() > 1200) {
							int max = tmp.getIconWidth() - 1200;
							int height = tmp.getIconHeight() - max;
							if (height < 700)
								height = 700;
							tmp = new ImageIcon(tmp.getImage().getScaledInstance(1200, height, Image.SCALE_DEFAULT));
							if (tmp.getIconHeight() > 850) {
								max = tmp.getIconHeight() - 850;
								int width = tmp.getIconWidth() - max;
								if (width < 900)
									height = 900;
								tmp = new ImageIcon(tmp.getImage().getScaledInstance(width, 850, Image.SCALE_DEFAULT));
							}

						}
						// rotate
						if (tmp.getIconWidth() > tmp.getIconHeight()) {

							tmp = new ImageIcon(rotate(90, tmp));

						}

						panel = new Pannello(tmp);
						piantina.add(panel,
								"dock north , width " + (tmp.getIconWidth() + 10) + ":" + (tmp.getIconWidth() + 10) + ":" + (tmp.getIconWidth() + 10)
										+ ", height " + (tmp.getIconHeight() + 10) + " :" + (tmp.getIconHeight() + 10) + ":" + (tmp.getIconHeight() + 10));

						replace = true;
						piantina.refresh();
					}
					// ovverride immagine
					else {
						// resize
						if (tmp.getIconWidth() > 1200) {
							int max = tmp.getIconWidth() - 1200;
							int height = tmp.getIconHeight() - max;
							if (height < 700)
								height = 700;
							tmp = new ImageIcon(tmp.getImage().getScaledInstance(1200, height, Image.SCALE_DEFAULT));
							if (tmp.getIconHeight() > 850) {
								max = tmp.getIconHeight() - 850;
								int width = tmp.getIconWidth() - max;
								if (width < 900)
									width = 900;
								tmp = new ImageIcon(tmp.getImage().getScaledInstance(width, 850, Image.SCALE_DEFAULT));
							}

						}
						panel.pulisci();

						// rotate
						if (tmp.getIconWidth() > tmp.getIconHeight()) {

							tmp = new ImageIcon(rotate(90, tmp));

						}

						panel.setSfondo(tmp);
						piantina.add(panel,
								"dock north , width " + (tmp.getIconWidth() + 10) + ":" + (tmp.getIconWidth() + 10) + ":" + (tmp.getIconWidth() + 10)
										+ ", height " + (tmp.getIconHeight() + 10) + " :" + (tmp.getIconHeight() + 10) + ":" + (tmp.getIconHeight() + 10));

						replace = true;
						piantina.refresh();
						paint(getGraphics());
					}
				} catch (Exception ex) {
					System.out.println(ex);
				}
			}
		};
		upload.addActionListener(uploadAct);
		upload.setToolTipText("Esporta Immagine");
		upload.setBackground(new Color(234, 234, 234));

		// P verso il basso
		JButton pdown = new JButton(new ImageIcon(ClassLoader.getSystemResource("pbdown.jpg")));
		pdown.setBorder(null);
		pdown.setContentAreaFilled(false);
		pdown.setRolloverEnabled(true);
		pdown.setRolloverIcon(new ImageIcon(ClassLoader.getSystemResource("pdown.jpg")));
		pdown.setToolTipText("PUNTO CRITICO");
		pdown.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(final ActionEvent arg0) {
				try {
					final Rel r = panel.addRel("P", BarraPulsanti.down, p);
				} catch (final Exception localException) {
				}
			}
		});

		// P verso l'alto
		JButton pup = new JButton(new ImageIcon(ClassLoader.getSystemResource("pbup.jpg")));
		pup.setBorder(null);
		pup.setContentAreaFilled(false);
		pup.setRolloverEnabled(true);
		pup.setToolTipText("PUNTO CRITICO");
		pup.setRolloverIcon(new ImageIcon(ClassLoader.getSystemResource("pup.jpg")));

		pup.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					Rel r = panel.addRel("P", BarraPulsanti.up, p);
				} catch (Exception localException) {
				}

			}
		});

		// P <-- sinistra
		JButton pleft = new JButton(new ImageIcon(ClassLoader.getSystemResource("pbleft.jpg")));
		pleft.setBorder(null);
		pleft.setContentAreaFilled(false);
		pleft.setRolloverEnabled(true);
		pleft.setToolTipText("PUNTO CRITICO");
		pleft.setRolloverIcon(new ImageIcon(ClassLoader.getSystemResource("pleft.jpg")));
		pleft.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					Rel r = panel.addRel("P", BarraPulsanti.left, p);
				} catch (Exception localException) {
				}
			}
		});

		// P --> destra
		JButton pright = new JButton(new ImageIcon(ClassLoader.getSystemResource("pbright.jpg")));
		pright.setBorder(null);
		pright.setContentAreaFilled(false);
		pright.setRolloverEnabled(true);
		pright.setToolTipText("PUNTO CRITICO");
		pright.setRolloverIcon(new ImageIcon(ClassLoader.getSystemResource("pright.jpg")));
		pright.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					Rel r = panel.addRel("P", BarraPulsanti.right, p);
				} catch (Exception localException) {
				}
			}
		});

		// P 45 gradi
		JButton p45 = new JButton(new ImageIcon(ClassLoader.getSystemResource("pb45.jpg")));
		p45.setBorder(null);
		p45.setContentAreaFilled(false);
		p45.setRolloverEnabled(true);
		p45.setToolTipText("PUNTO CRITICO");
		p45.setRolloverIcon(new ImageIcon(ClassLoader.getSystemResource("p45.jpg")));
		p45.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					Rel r = panel.addRel("P", BarraPulsanti.degree45, p);
				} catch (Exception localException) {
				}
			}
		});

		// P 135 gradi
		JButton p135 = new JButton(new ImageIcon(ClassLoader.getSystemResource("pb135.jpg")));
		p135.setBorder(null);
		p135.setContentAreaFilled(false);
		p135.setRolloverEnabled(true);
		p135.setToolTipText("PUNTO CRITICO");
		p135.setRolloverIcon(new ImageIcon(ClassLoader.getSystemResource("p135.jpg")));
		p135.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					Rel r = panel.addRel("P", BarraPulsanti.degree135, p);
				} catch (Exception localException) {
				}
			}
		});

		// P 225 gradi
		JButton p225 = new JButton(new ImageIcon(ClassLoader.getSystemResource("pb225.jpg")));
		p225.setBorder(null);
		p225.setContentAreaFilled(false);
		p225.setRolloverEnabled(true);
		p225.setToolTipText("PUNTO CRITICO");
		p225.setRolloverIcon(new ImageIcon(ClassLoader.getSystemResource("p225.jpg")));
		p225.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					Rel r = panel.addRel("P", BarraPulsanti.degree225, p);
				} catch (Exception localException) {
				}
			}
		});

		// P 315 gradi
		JButton p315 = new JButton(new ImageIcon(ClassLoader.getSystemResource("pb315.jpg")));
		p315.setBorder(null);
		p315.setContentAreaFilled(false);
		p315.setRolloverEnabled(true);
		p315.setToolTipText("PUNTO CRITICO");
		p315.setRolloverIcon(new ImageIcon(ClassLoader.getSystemResource("p315.jpg")));
		p315.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					Rel r = panel.addRel("P", BarraPulsanti.degree315, p);
				} catch (Exception localException) {
				}
			}
		});

		// M verso il basso
		JButton mdown = new JButton(new ImageIcon(ClassLoader.getSystemResource("mbdown.jpg")));
		mdown.setBorder(null);
		mdown.setContentAreaFilled(false);
		mdown.setRolloverEnabled(true);
		mdown.setToolTipText("PUNTO DI MISURAZIONE");
		mdown.setRolloverIcon(new ImageIcon(ClassLoader.getSystemResource("mdown.jpg")));
		mdown.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				Rel r;
				try {
					r = panel.addRel("M" + (countM++), BarraPulsanti.down, BarraPulsanti.m);
				} catch (Exception localException) {
				}
			}
		});

		// M verso alto
		JButton mup = new JButton(new ImageIcon(ClassLoader.getSystemResource("mbup.jpg")));
		mup.setBorder(null);
		mup.setContentAreaFilled(false);
		mup.setRolloverEnabled(true);
		mup.setToolTipText("PUNTO DI MISURAZIONE");
		mup.setRolloverIcon(new ImageIcon(ClassLoader.getSystemResource("mup.jpg")));
		mup.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				Rel r;
				try {
					r = panel.addRel("M" + (countM++), BarraPulsanti.up, BarraPulsanti.m);
				} catch (Exception localException) {
				}
			}
		});

		// M verso <--
		JButton mleft = new JButton(new ImageIcon(ClassLoader.getSystemResource("mbleft.jpg")));
		mleft.setBorder(null);
		mleft.setContentAreaFilled(false);
		mleft.setRolloverEnabled(true);
		mleft.setToolTipText("PUNTO DI MISURAZIONE");
		mleft.setRolloverIcon(new ImageIcon(ClassLoader.getSystemResource("mleft.jpg")));
		mleft.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				Rel r;
				try {
					r = panel.addRel("M" + (countM++), BarraPulsanti.left, BarraPulsanti.m);
				} catch (Exception localException) {
				}
			}
		});

		// M verso -->
		JButton mright = new JButton(new ImageIcon(ClassLoader.getSystemResource("mbright.jpg")));
		mright.setBorder(null);
		mright.setContentAreaFilled(false);
		mright.setRolloverEnabled(true);
		mright.setToolTipText("PUNTO DI MISURAZIONE");
		mright.setRolloverIcon(new ImageIcon(ClassLoader.getSystemResource("mright.jpg")));
		mright.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				Rel r;
				try {
					r = panel.addRel("M" + (countM++), BarraPulsanti.right, BarraPulsanti.m);
				} catch (Exception localException) {
				}
			}
		});

		// bottone per scegliere c
		final JButton c = new JButton(new ImageIcon(ClassLoader.getSystemResource("clima.jpg")));
		c.setBorder(null);
		c.setContentAreaFilled(false);
		c.setToolTipText("PUNTO RILIEVO CLIMA");

		// menu list
		final JPopupMenu cMenu = new JPopupMenu();
		// inerno
		JMenuItem ci = new JMenuItem("Clima Interno");
		ci.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				try {
					Rel r = panel.addRel("CI", BarraPulsanti.none, BarraPulsanti.c);
				} catch (Exception localException) {
				}

			}
		});
		// esterno
		JMenuItem ce = new JMenuItem("Clima Esterno");
		ce.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				try {
					Rel r = panel.addRel("CE", BarraPulsanti.none, BarraPulsanti.c);
				} catch (Exception localException) {
				}

			}
		});
		// inerrato
		JMenuItem cc = new JMenuItem("Clima Interrato");
		cc.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				try {
					Rel r = panel.addRel("CC", BarraPulsanti.none, BarraPulsanti.c);
				} catch (Exception localException) {
				}

			}
		});
		// aggiungi alla lista
		cMenu.add(ci);
		cMenu.add(ce);
		cMenu.add(cc);
		// visualiza quando clicco su c
		c.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				cMenu.show(e.getComponent(), e.getX(), e.getY());
			}
		});

		// bottone per quota
		JButton q = new JButton(new ImageIcon(ClassLoader.getSystemResource("quota.jpg")));
		q.setBorder(null);
		q.setContentAreaFilled(false);
		q.setToolTipText("QUOTE PAVIMENTI");
		q.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				Rel r;
				try {
					r = panel.addRel("Q", BarraPulsanti.none, BarraPulsanti.q);
				} catch (Exception localException) {
				}
			}
		});

		// T verso basso
		JButton tdown = new JButton(new ImageIcon(ClassLoader.getSystemResource("tbdown.jpg")));
		tdown.setBorder(null);
		tdown.setContentAreaFilled(false);
		tdown.setRolloverEnabled(true);
		tdown.setToolTipText("TENSIONE VERTICALE - TENSIONE ORIZZONTALE");
		tdown.setRolloverIcon(new ImageIcon(ClassLoader.getSystemResource("tdown.jpg")));
		tdown.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				Rel r;
				try {
					r = panel.addRel("TV-TO", BarraPulsanti.down, BarraPulsanti.t);
				} catch (Exception localException) {
				}
			}
		});

		// T verso alto
		JButton tup = new JButton(new ImageIcon(ClassLoader.getSystemResource("tbup.jpg")));
		tup.setBorder(null);
		tup.setContentAreaFilled(false);
		tup.setRolloverEnabled(true);
		tup.setToolTipText("TENSIONE VERTICALE - TENSIONE ORIZZONTALE");
		tup.setRolloverIcon(new ImageIcon(ClassLoader.getSystemResource("tup.jpg")));
		tup.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				Rel r;
				try {
					r = panel.addRel("TV-TO", BarraPulsanti.up, BarraPulsanti.t);
				} catch (Exception localException) {
				}
			}
		});

		// T verso <--
		JButton tleft = new JButton(new ImageIcon(ClassLoader.getSystemResource("tbleft.jpg")));
		tleft.setBorder(null);
		tleft.setContentAreaFilled(false);
		tleft.setRolloverEnabled(true);
		tleft.setToolTipText("TENSIONE VERTICALE - TENSIONE ORIZZONTALE");
		tleft.setRolloverIcon(new ImageIcon(ClassLoader.getSystemResource("tleft.jpg")));
		tleft.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				Rel r;
				try {
					r = panel.addRel("TV-TO", BarraPulsanti.left, BarraPulsanti.t);
				} catch (Exception localException) {
				}
			}
		});

		// T verso -->
		JButton tright = new JButton(new ImageIcon(ClassLoader.getSystemResource("tbright.jpg")));
		tright.setBorder(null);
		tright.setContentAreaFilled(false);
		tright.setRolloverEnabled(true);
		tright.setToolTipText("TENSIONE VERTICALE - TENSIONE ORIZZONTALE");
		tright.setRolloverIcon(new ImageIcon(ClassLoader.getSystemResource("tright.jpg")));
		tright.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				Rel r;
				try {
					r = panel.addRel("TV-TO", BarraPulsanti.right, BarraPulsanti.t);
				} catch (Exception localException) {
				}
			}
		});

		// bottone per nord
		final JButton nord = new JButton("NORD");
		nord.setBorder(null);
		nord.setToolTipText("DIREZIONE NORD");

		// popup menu nord per scelta
		final JPopupMenu nordMenu = new JPopupMenu();

		// up
		JMenuItem upNord = new JMenuItem(new ImageIcon(ClassLoader.getSystemResource("up_nord.jpg")));
		upNord.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				try {
					Rel r = panel.addRel("N", BarraPulsanti.up, BarraPulsanti.n);
				} catch (Exception localException) {
				}

			}
		});
		nordMenu.add(upNord);

		// down
		JMenuItem downNord = new JMenuItem(new ImageIcon(ClassLoader.getSystemResource("down_nord.jpg")));
		downNord.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				try {
					Rel r = panel.addRel("N", BarraPulsanti.down, BarraPulsanti.n);
				} catch (Exception localException) {
				}

			}
		});
		nordMenu.add(downNord);

		// left
		JMenuItem leftNord = new JMenuItem(new ImageIcon(ClassLoader.getSystemResource("left_nord.jpg")));
		leftNord.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				try {
					Rel r = panel.addRel("N", BarraPulsanti.left, BarraPulsanti.n);
				} catch (Exception localException) {
				}

			}
		});
		nordMenu.add(leftNord);

		// right
		JMenuItem rightNord = new JMenuItem(new ImageIcon(ClassLoader.getSystemResource("right_nord.jpg")));
		rightNord.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				try {
					Rel r = panel.addRel("N", BarraPulsanti.right, BarraPulsanti.n);
				} catch (Exception localException) {
				}

			}
		});
		nordMenu.add(rightNord);

		// 45
		JMenuItem d45Nord = new JMenuItem(new ImageIcon(ClassLoader.getSystemResource("45_nord.jpg")));
		d45Nord.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				try {
					Rel r = panel.addRel("N", BarraPulsanti.degree45, BarraPulsanti.n);
				} catch (Exception localException) {
				}

			}
		});
		nordMenu.add(d45Nord);

		// 135
		JMenuItem d135Nord = new JMenuItem(new ImageIcon(ClassLoader.getSystemResource("135_nord.jpg")));
		d135Nord.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				try {
					Rel r = panel.addRel("N", BarraPulsanti.degree135, BarraPulsanti.n);
				} catch (Exception localException) {
				}

			}
		});
		nordMenu.add(d135Nord);

		// 225
		JMenuItem d225Nord = new JMenuItem(new ImageIcon(ClassLoader.getSystemResource("225_nord.jpg")));
		d225Nord.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				try {
					Rel r = panel.addRel("N", BarraPulsanti.degree225, BarraPulsanti.n);
				} catch (Exception localException) {
				}

			}
		});
		nordMenu.add(d225Nord);

		// 315
		JMenuItem d315Nord = new JMenuItem(new ImageIcon(ClassLoader.getSystemResource("315_nord.jpg")));
		d315Nord.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				try {
					Rel r = panel.addRel("N", BarraPulsanti.degree315, BarraPulsanti.n);
				} catch (Exception localException) {
				}

			}
		});
		nordMenu.add(d315Nord);

		nord.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				nordMenu.show(e.getComponent(), e.getX(), e.getY());
			}
		});

		add(upload);

		add(pup);
		add(pdown);
		add(pleft);
		add(pright);
		add(p45);
		add(p135);
		add(p225);
		add(p315);

		add(mup);
		add(mdown);
		add(mleft);
		add(mright);

		add(c);

		add(q);

		add(tup);
		add(tdown);
		add(tleft);
		add(tright);
		add(nord, "wrap");

		add(new JLabel("Descrizione piano"));
		piano.setPreferredSize(new Dimension(400, 25));
		JButton apply = new JButton("Rinomina pianta", new ImageIcon(ClassLoader.getSystemResource("invia-file.png")));
		apply.setBorder(null);
		apply.setContentAreaFilled(false);
		apply.setRolloverEnabled(true);
		apply.setRolloverIcon(new ImageIcon(ClassLoader.getSystemResource("invia-file_roll.png")));
		apply.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				if (!piano.getText().equals(""))
					piantina.setTitle(piano.getText());
			}
		});
		add(piano, "span 11");
		add(apply, "span");
	}

	public void setElement(String data) {

		ImageIcon tmp = new ImageIcon(data);
		panel = new Pannello(tmp);
		try {
			panel.pulisci();
			piantina.remove(panel);
		} catch (Exception e) {
			// TODO: handle exception
		}
		replace = true;
		piantina.add(panel, "dock north , width " + (tmp.getIconWidth() + 10) + ":" + (tmp.getIconWidth() + 10) + ":" + (tmp.getIconWidth() + 10)
				+ ", height " + (tmp.getIconHeight() + 10) + " :" + (tmp.getIconHeight() + 10) + ":" + (tmp.getIconHeight() + 10));
		piantina.refresh();

	}

	public Pannello getpanel() {
		return panel;
	}

	/**
	 * ruota l'immagine
	 * 
	 * @param degs
	 *           = gradi
	 * @param prev
	 *           = immagine da girare
	 * @return nuova immagine
	 */
	private Image rotate(double degs, ImageIcon prev) {
		ImageIcon img = prev; // why an ImageIcon???
		Image temp = new BufferedImage(img.getIconHeight(), img.getIconWidth(), BufferedImage.TYPE_INT_RGB);

		Graphics2D g2d = (Graphics2D) temp.getGraphics();
		AffineTransform trans = new AffineTransform(0.0, 1.0, -1.0, 0.0, img.getIconHeight(), 0.0);
		g2d.transform(trans);
		g2d.drawImage(img.getImage(), 0, 0, img.getIconWidth(), img.getIconHeight(), null);
		g2d.dispose();
		return temp;
	}

}